from datetime import datetime

from dateutil.relativedelta import relativedelta
from sqlalchemy import Column, Integer, String, Boolean, DateTime, Table, ForeignKey, or_, and_
from werkzeug.security import generate_password_hash, check_password_hash

from configs import GUEST_GROUP_ID, GUEST_DEFAULT_VALIDITY_PERIOD, GROUP_DEFAULT_VALIDITY_PERIOD
from db.base import Base
from db.models.base import BaseDatabaseModel


class User(Base, BaseDatabaseModel):
	__tablename__ = "users"

	id = Column(Integer, primary_key=True, index=True, comment="整型id")
	username = Column(String(50), unique=True, index=True, comment="用户名称")
	role = Column(String(20), default="normal", index=True, comment="用户角色：admin > superman > normal")
	nickname = Column(String(50), comment="昵称")
	password = Column(String(255), comment="加密后的密码")
	token = Column(String(1024), nullable=True, comment="chat时验证过期")
	wx_token = Column(String(1024), nullable=True, comment="微信的验证过期")
	is_delete = Column(Boolean, default=False, comment="逻辑删除")
	company = Column(String(50), nullable=True, comment="公司名称")
	note = Column(String(1024), nullable=True, comment="对于用户的备注")
	is_admin = Column(Boolean, default=False)

	def encry_password(self):
		# 加密密码
		self.password = generate_password_hash(self.password)  # noqa

	def verify_password(self, plain_text_password):
		# 验证密码
		return check_password_hash(self.password, plain_text_password)

	def check_admin_role(self):
		if self.role in ["admin", "superman"]:
			return True
		return False

	def check_superman_role(self):
		if self.role == "superman":
			return True
		return False

	def to_json(self):
		return {
			"username": self.username,
			"id": self.id,
			"is_admin": self.is_admin,
			"nickname": self.nickname,
			"role": self.role,
			"company": self.company,
			"note": self.note
		}


class Group(Base, BaseDatabaseModel):
	__tablename__ = "groups"
	id = Column(Integer, primary_key=True, index=True, comment="")
	name = Column(String(50), index=True, comment="")
	description = Column(String(1000), comment="")
	is_delete = Column(Boolean, default=False, comment="")
	expiration_time = Column(DateTime, default=datetime.now() + relativedelta(years=GROUP_DEFAULT_VALIDITY_PERIOD), comment="")
	owner = Column(Integer, ForeignKey('users.id'), comment="")

	def to_json(self):
		return {
			"id": self.id,
			"name": self.name,
			"owner": self.owner,
			"description": self.description,
			"expiration_time": self.expiration_time.timestamp()
		}


class UserGroup(BaseDatabaseModel, Base):
	__tablename__ = "user_group"
	id = Column(Integer, primary_key=True, index=True, comment="")
	user = Column(Integer, ForeignKey('users.id'), comment="")
	group = Column(Integer, ForeignKey('groups.id'), comment="")


class GroupPermission(BaseDatabaseModel, Base):
	__tablename__ = "group_permission"
	id = Column(Integer, primary_key=True, index=True, comment="")
	permission = Column(String(20), index=True, comment="")
	group = Column(Integer, ForeignKey('groups.id'), comment="")

